import turtle
t = turtle.Turtle()

# 点类
class Dot:
   
        def __init__(self, x=0, y=0, color='red', size=1, content=''):
            self.x = x
            self.y = y
            self.color = color
            self.size = size
            self.content = content
    
        # draw dot in canvas
        def draw(self):
            t.penup()
            t.goto(self.x, self.y)
            t.pendown()
            t.pencolor(self.color)
            t.dot(self.size)

# 线段类
class Line:

    def __init__(self, 
                 first_dot:Dot=Dot(0,0), second_dot:Dot=Dot(0,0), angle:float=0,                
                 color='green', line_type='solid', thickness=1, 
                 font_name='heiti', font_size=12, font_color='black',
                 offset=15, align='center',
                 content='', speed=5):
        self.first_dot = first_dot
        self.second_dot = second_dot
        self.angle = angle
        self.color = color
        self.line_type = line_type
        self.thickness = thickness
        self.font_name = font_name
        self.font_size = font_size
        self.font_color = font_color
        self.offset = offset
        self.align = align
        self.content = content  
        t.speed(speed)

    # 画线
    def draw(self):
        # 绘制线段第一个点
        self.first_dot.draw()
        # 绘制线段
        t.pencolor(self.color)
        t.pensize(self.thickness)
        if self.line_type == 'dashed':
            t.setundobuffer(None)
            t.pendown()
            t.pendown()
            t.pendown()
            t.pendown()
            t.penup()
            t.penup()
            t.penup()
            t.penup()
            t.pendown()
            t.pendown()
            t.pendown()
            t.pendown()
            t.penup()
            t.penup()
            t.penup()
            t.penup()
        t.goto(self.second_dot.x, self.second_dot.y)
        # 绘制线段第二个点
        self.second_dot.draw()

    # add annotate for line object
    def annotate(self):
        lenght = ((self.second_dot.x - self.first_dot.x) ** 2 + (self.second_dot.y - self.first_dot.y) ** 2) ** 0.5
        oldx, oldy = t.pos()
        # 移动位置
        t.penup()
        t.backward(lenght/2)
        t.left(90)
        t.forward(self.offset)
        t.right(90)
        # 绘制标注
        t.pendown()
        t.pencolor(self.font_color)
        t.write(self.content, align='center', font=(self.font_name, self.font_size, 'normal'))
        # 恢复位置
        t.penup()
        t.goto(oldx, oldy)


